package controllers

import (
	"encoding/json"
	"fmt"
	resourceFileDao "geological_data_mgr/daos/resourceFile"
	. "geological_data_mgr/requests/common"
	. "geological_data_mgr/requests/resourceFile"
	. "geological_data_mgr/utils"
	"github.com/beego/beego/v2/server/web"
)

// Operations about ResourceFile
type ResourceFileController struct {
	web.Controller
}

// @Title 创建 ResourceFile
// @Description 创建 资源文件
// @Param	body		body 	models.ResourceFile	true		"body for resourceFile content"
// @Success 200 {int} models.ResourceFile.Id
// @Failure 403 body is empty
// @router /geological_data_mgr/resourcefile/createObj [post]
func (this *ResourceFileController) CreateObj() {
	var err error
	var request CreateObj
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		if request.Token == nil {
			request.Token = Uuid()
		} else {
			resourceFileDao.DeleteObj(request.Token)
		}
		resourceFile := request.Convert2ResourceFile()
		if _, err := resourceFileDao.CreateObj(&resourceFile); err == nil {
			this.Data["json"] = map[string]interface{}{"code": 0}
		} else {
			Logger.Warn(fmt.Sprintf("CreateObj %s", err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
	}
	this.ServeJSON()
}

// @Title getObjById
// @Description 根据Id获取资源文件
// @Param	body		body 	string	true		"body for resourceFile content"
// @Success 200 {int} models.ResourceFile.Id
// @Failure 403 body is empty
// @router /getObjById [post]
func (this *ResourceFileController) GetObjById() {
	var err error
	var request GetById
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		if obj, err := resourceFileDao.GetObjById(request.Id); err == nil {
			this.Data["json"] = map[string]interface{}{"value": obj, "code": 0}
		} else {
			Logger.Warn(fmt.Sprintf("GetObjById %s %s", StringPtrAsTxt(request.Id), err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
	}
	this.ServeJSON()
}

// @Title updateObj
// @Description 更新资源文件
// @Param	body		body 	models.ResourceFile	true		"body for resourceFile content"
// @Success 200 {int} models.ResourceFile.Id
// @Failure 403 body is empty
// @router /updateObj [post]
func (this *ResourceFileController) UpdateObj() {
	var err error
	var request UpdateObj
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		resourceFile := request.Convert2ResourceFile()
		if _, err := resourceFileDao.UpdateObj(&resourceFile); err == nil {
			this.Data["json"] = map[string]interface{}{"code": 0}
		} else {
			Logger.Warn(fmt.Sprintf("UpdateObj %s", err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
	}
	this.ServeJSON()
}

// @Title listObj
// @Description 列出 资源文件
// @Param	body		body 	models.ResourceFile	true		"body for resourceFile content"
// @Success 200 {int} models.ResourceFile.Id
// @Failure 403 body is empty
// @router /listObj [post]
func (this *ResourceFileController) ListObj() {
	if list, err := resourceFileDao.ListObj(); err == nil {
		this.Data["json"] = map[string]interface{}{"code": 0, "list": list}
	} else {
		Logger.Warn(fmt.Sprintf("ListObj %s", err.Error()))
		this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
	}
	this.ServeJSON()
}

// @Title Count4Page
// @Description 分页方式列出资源文件
// @Param	body		body 	models.ResourceFile	true		"body for resourceFile content"
// @Success 200 {int} models.ResourceFile.Id
// @Failure 403
// @router /pageObj [post]
func (this *ResourceFileController) Count4Page() {
	var err error
	var request SearchInfo
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		if count, err := resourceFileDao.Count4Page(request.GetConditions()); err == nil {
			this.Data["json"] = map[string]interface{}{"code": 0, "count": count}
		} else {
			Logger.Warn(fmt.Sprintf("Count4Page %s", err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
	}
	this.ServeJSON()
	return
}

// @Title PageObj
// @Description 分页方式列出资源文件
// @Param	body		body 	models.ResourceFile	true		"body for resourceFile content"
// @Success 200 {int} models.ResourceFile.Id
// @Failure 403
// @router /pageObj [post]
func (this *ResourceFileController) PageObj() {
	var err error
	var request PageObj
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		if list, err := resourceFileDao.PageObj(request.SearchInfo.GetConditions(), request.GetOrder(), request.PageInfo.Page, request.PageInfo.PageSize); err == nil {
			this.Data["json"] = map[string]interface{}{"code": 0, "list": list}
		} else {
			Logger.Warn(fmt.Sprintf("PageObj %s", err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
	}
	this.ServeJSON()
	return
}

// @Title DeleteObj
// @Description 删除资源文件
// @Param	body		body 	models.ResourceFile	true		"body for resourceFile content"
// @Success 200 {int}
// @Failure 403
// @router /deleteObj [post]
func (this *ResourceFileController) DeleteObj() {
	var err error
	var request GetById
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		if count, err := resourceFileDao.DeleteObj(request.Id); err == nil {
			this.Data["json"] = map[string]interface{}{"code": 0, "count": count}
		} else {
			Logger.Warn(fmt.Sprintf("DeleteObj %s", err.Error()))
			this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
		}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
	}
	this.ServeJSON()
	return
}

// @Title deleteObjs
// @Description create 资源文件
// @Param	body		body 	models.ResourceFile	true		"body for resourceFile content"
// @Success 200 {int} models.ResourceFile.Id
// @Failure 403 body is empty
// @router /deleteObjs [delete]
func (this *ResourceFileController) DeleteObjs() {
	var err error
	var request IdsReq
	count := 0
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &request); err == nil {
		for _, id := range request.Ids {
			if _, err = resourceFileDao.DeleteObj(id); err != nil {
				Logger.Warn(fmt.Sprintf("DeleteObjs %s", err.Error()))
				this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
				this.ServeJSON()
				return
			}
			count++
		}
		this.Data["json"] = map[string]interface{}{"code": 0, "count": count}
	} else {
		this.Data["json"] = map[string]interface{}{"error": err.Error(), "code": -1}
	}
	this.ServeJSON()
	return
}

func init() {
	web.Router("/geological_data_mgr/resourcefile/listObj", &ResourceFileController{}, "get,post:ListObj")
	web.Router("/geological_data_mgr/resourcefile/deleteObjs", &ResourceFileController{}, "post:DeleteObjs")
	web.Router("/geological_data_mgr/resourcefile/deleteObj", &ResourceFileController{}, "post:DeleteObj")
	web.Router("/geological_data_mgr/resourcefile/pageObj", &ResourceFileController{}, "post:PageObj")
	web.Router("/geological_data_mgr/resourcefile/count4Page", &ResourceFileController{}, "post:Count4Page")
	web.Router("/geological_data_mgr/resourcefile/updateObj", &ResourceFileController{}, "post:UpdateObj")
	web.Router("/geological_data_mgr/resourcefile/getObjById", &ResourceFileController{}, "post:GetObjById")
	web.Router("/geological_data_mgr/resourcefile/createObj", &ResourceFileController{}, "post:CreateObj")
}
